<?php
require_once('config.php');
require_once('db.php');
if (isset($_POST['url']))
	$url = $_POST['url'];
else
	return;
if (isset($_POST['pause']))
	$pause = $_POST['pause'];
else
	return;
if (isset($_POST['id']))
	$id = $_POST['id'];
else
	return;
if (isset($_POST['newp']))
	$newp=$_POST['newp'];

$message = "";
// Connect to server and select database.
$link = connectdb($servername,$dbname,$dbusername,$dbpassword);
// update data in mysql database
$sql="UPDATE $dbSitesTabale SET hostname ='$url', pause = '$pause' WHERE id='$id'";
$result=mysql_query($sql);
if(!$result){	
	$message .= "There is an MySQL error occurred: <b>" . mysql_error($link) . "</b><br />";
		
}

if (isset($_POST['hidCheck']))
$checkbox=$_POST['hidCheck'];

if($checkbox > 0)
{
	//14,16 -> '14','16'
	$sql = "DELETE FROM site_protocol WHERE (site_id = '";
	$sql .= $id . "') AND (protocol_id in (" . $checkbox . "))";
	//echo $sql;
	echo "<br>";
	$result = mysql_query($sql);
	if(!$result){	
		$message .= "There is an MySQL error occurred: <b>" . mysql_error($link) . "</b><br />";			
	}	
	
}

if(isset($_POST['showCheck']) && $_POST['showCheck'] > 0){
	$showList = explode(',', $_POST['showCheck']);	
	foreach($showList as $protocol_id){
		$sql = "UPDATE " . $dbSiteProtocolsTable 
		. " SET checking_rate = " . $_POST[$id . "-" . $protocol_id]
		. " WHERE "
		. " site_id = " . $id  
		. " and protocol_id = " . $protocol_id;
		$result = mysql_query($sql);
		if(!$result){	
			$message .= "There is an MySQL error occurred: <b>" . mysql_error($link) . "</b><br />";
				
		}	
	}
}
while (list ($key2,$val2) = @each ($newp)) {
	$sql = "INSERT INTO " . $dbSiteProtocolsTable . "(site_id, protocol_id, checking_rate) VALUES ('";
	$sql .= $id . "', '" . $val2 . "'," . $_POST[$id . "-" . $val2] . ")";
	//echo $sql;
	$result = mysql_query($sql);
	if(!$result){	
		$message .= "There is an MySQL error occurred: <b>" . mysql_error($link) . "</b><br />";
	
	}	
}

$message .= urldecode("<b>Update completed</b>");
close($link);
header("Location: updatesite.php?id=$id&message=" . $message);		


?>